<template>
	<view class="page">
		<view class="top-box">
			<cus-tabs v-if="typeData.length>0" class="tab-box" :list="typeData" :showBar="false" :current="typeIndex"
				activeColor="#d01919" @change="typeChangeHandle">
			</cus-tabs>
			<cus-tabs class="tab-box" v-if="minorData.length>0" :list="minorData" :showBar="false" :current="minorIndex"
				activeColor="#d01919" @change="minorChangeHandle"></cus-tabs>
		</view>

		<com-novel-list :list="items"></com-novel-list>
	</view>
</template>

<script>
	import {
		mapGetters
	}
	from 'vuex'
	export default {
		data() {
			return {
				typeIndex: 0,
				minorIndex: 0,
				typeData: [],
				minorData: [],
				option: {},
				tabsH: 0,

				curPage: 1,
				pageLimit: 20,
				loadMoreStatus: 0, //加载更多 0加载前，1加载中，2没有更多了
				items: [],
			}
		},
		computed: {
			...mapGetters({
				'curSource': 'get_curSource'
			}),
		},
		onLoad(option) {
			this.typeData = this.curSource.typeData || []

			uni.setNavigationBarTitle({
				title: option.genderName
			})
			this.option = option
			this.getBookList('refresh')
		},
		methods: {
			getCategoryLevel(option) {
				return new Promise(async (resolve, reject) => {
					let params = option.href
					let res = await this.$store.dispatch('getCategoryLevel', params)
					if (res) {
						resolve(res)
					}
				})
			},
			async getBookList(type) {
				let params = ''
				switch (this.curSource.value) {
					case 'zhuishushenqi':
						if (this.minorData.length === 0) {
							this.minorData = await this.getCategoryLevel(this.option)
							this.tabsH = 90
						}
						params = {
							gender: this.option.gender,
							type: this.typeData[this.typeIndex].value,
							query: this.option.major,
							cat: this.minorData[this.minorIndex].replace('全部', ''),
							start: (this.curPage - 1) * this.pageLimit,
							limit: this.pageLimit,
							packageName: '',
							channelName: ''
						}
						break
					case 'xbiquge':
						params = this.option.href + '_' + this.curPage + '.html'
						break
				}
				let res = await this.$store.dispatch('getBookList', params)
				if (res) {
					if (type === 'refresh') {
						this.items = res.books
					}
					if (type === 'add') {
						this.items.push(...res.books)
					}
					this.loadMoreStatus = res.pages > this.curPage ? 0 : 2
				}
			},
			typeChangeHandle(e) {
				this.typeIndex = e
				this.getBookList('refresh')
			},
			minorChangeHandle(e) {
				this.minorIndex = e
				this.getBookList('refresh')
			},
			async novelTap(item) {
				uni.navigateTo({
					url: '/pages/public/bookDetail?id=' + item._id
				})
			},
			scrolltolower() {
				if (this.loadMoreStatus === 2) {
					return;
				}
				this.loadMoreStatus = 1;
				this.curPage += 1
				this.getBookList('add')
			},
		}
	}
</script>
<style lang="scss">
	.top-box {
		position: sticky;
		top: 0;
		z-index: 999;

		.tab-box {
			padding: 0 15px;
			border-bottom: 1px solid #ddd;
		}
	}
</style>
